﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DreamBuilder.Common;
using System.Data;
using DreamBuilder.Core.DAO;
using DreamBuilder.Core;

namespace Web.SystemConfig.DataAccess
{
    class DALAuth_UserState : DataAccessBase
    {
        IDBHelper helper = null;
        public DALAuth_UserState()
        {
            if (this.helper == null)
            {
                this.helper = DBHelperFactory.CreateInstance(DbHelperName.Business);
            }
        }
        /// <summary>
        /// 检查用户在线状态
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public bool CheckLoginStatus(string UserId)
        {
            string sql = "select UserState from P_Auth_UserState where F_UserId='"+UserId+"'";
            using (IDataReader dr = this.helper.ExecuteReader(sql))
            {
                if (dr.Read())
                {
                    return Convert.ToBoolean(dr[0]);
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 检查用户是否在用户状态表中
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public bool CheckUserStateExsist(string UserId)
        {
            string sql = "select count(*) from  P_Auth_UserState where F_UserId='" + UserId + "'";
            object obj = this.helper.ExecuteScalar(sql);
            int i = Convert.ToInt32(obj);
            if (i>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 增加用户到用户状态表(插入的时候为登陆状态)
        /// </summary>
        /// <param name="UserId">用户ID</param>
        /// <param name="RealID">真实ID</param>
        /// <param name="patform">平台信息</param>
        /// <param name="loginIp">登陆IP</param>
        /// <param name="browers">浏览器信息</param>
        /// <returns></returns>
        public bool AddUserToUserState(string UserId,string RealID,string patform,string loginIp,string browers)
        {
            string sql = string.Empty;
            if (DBHelper.database is Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase)
            {
                sql = "INSERT INTO P_Auth_UserState (REC_ID,F_UserId,RealUserId,UserState,LoginIP,LoginTime,LoginPlatform,LoginBrowers) VALUES ('" + Guid.NewGuid().ToString() + "','" + UserId + "','" + RealID + "',1,'" + loginIp + "',TO_DATE('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy=mm-dd HH24:mi:ss'),'" + patform + "','" + browers + "')";
            }
            else
            {
                sql = "INSERT INTO P_Auth_UserState (REC_ID,F_UserId,RealUserId,UserState,LoginIP,LoginTime,LoginPlatform,LoginBrowers) VALUES ('" + Guid.NewGuid().ToString() + "','" + UserId + "','" + RealID + "',1,'" + loginIp + "','" + DateTime.Now + "','" + patform + "','" + browers + "')";
            }
            return this.helper.ExecuteSql(sql) > 0 ? true : false ;
        }

        /// <summary>
        /// 查询所有在线用户
        /// </summary>
        /// <returns></returns>
        public DataTable GetOnlineUser()
        {
            string sql = "select a.*,b.UserState from P_Auth_UserState as b , P_Auth_User as a where b.UserState=1 and b.F_UserId=a.User_ID ";
            return this.helper.ExecuteDataSet(sql).Tables[0];
        }

        /// <summary>
        /// 更改用户在线状态
        /// </summary>
        /// <param name="UserId">用户ID</param>
        /// <param name="RealID">真实ID</param>
        /// <param name="patform">平台信息</param>
        /// <param name="loginIp">登陆IP地址</param>
        /// <param name="browers">浏览器信息</param>
        /// <param name="onlineState">在线状态</param>
        /// <returns></returns>
        public bool UpdateUserState(string UserId,string RealID,string patform,string loginIp,string browers,bool onlineState)
        {
            int i = Convert.ToInt32(onlineState);
            string sql = "Update P_Auth_UserState set UserState=" + i + ",RealUserId='" + RealID + "' where F_UserId='" + UserId + "'";
            return  this.helper.ExecuteSql(sql) > 0 ? true : false;
        }

        /// <summary>
        /// 获取当前在线人数
        /// </summary>
        /// <returns></returns>
        public int GetOnlineUserNumber()
        {
            string sql = "select count(*) from P_Auth_UserState  where UserState=1 ";
            return Convert.ToInt32(this.helper.ExecuteScalar(sql));
        }
    }
}
